package com.goldentrail.goldenapp.server;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;

import com.goldentrail.goldenapp.shared.Constants;


public class FileUploadServlet extends HttpServlet implements Constants {
	 
	     @Override
	     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	             throws ServletException, IOException {
		         super.doGet(req, resp);
	     }
	 
	     @Override
	     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	             throws ServletException, IOException {
	         System.out.println("Entering post function.");
	         // process only multipart requests
	         if (ServletFileUpload.isMultipartContent(req)) {
	 
	             // Create a factory for disk-based file items
	             FileItemFactory factory = new DiskFileItemFactory();
	 
	             // Create a new file upload handler
	             ServletFileUpload upload = new ServletFileUpload(factory);
	 
	             // Parse the request
	             try {
	                 List<FileItem> items = upload.parseRequest(req);
	                 System.out.println("Parsing the form field...");
	                 for (FileItem item : items) {
	                     // process only file upload - discard other form item types
	                     if (item.isFormField()) continue;
	                     
	                     System.out.println("Form Field found.");
	                     
	                     String fileName = item.getName();
	                     // get only the file name not whole path
	                     if (fileName != null) {
	                         fileName = FilenameUtils. getName(fileName);
	                     }
	                     System.out.println(UPLOAD_DIRECTORY + "\n" + fileName);
	                     File uploadedFile = new File(UPLOAD_DIRECTORY, fileName);
	                     if (uploadedFile.createNewFile()) {
	                         item.write(uploadedFile);
	                         resp.setStatus(HttpServletResponse.SC_CREATED);
	                         resp.getWriter().print("The file was created successfully.");
	                         resp.flushBuffer();
	                     }
	                     else{
	                         resp.setStatus(HttpServletResponse.SC_CREATED);
                         	 resp.getWriter().print("The file already exists in repository.");
                         	 resp.flushBuffer();
	                     }
	                 }
	             } catch (Exception e) {
	                 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
	                         "An error occurred while creating the file : " + e.getMessage());
	             }
	 
	         } else {
	             resp.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE,
	                             "Request contents type is not supported by the servlet.");
	         }
	     }
}
